package gui;

/**
 * @author Ramy Refky 
 * */
public class NodeComponentGraphics extends ComponentGraphics{

	private static final long serialVersionUID = 1L;
	
	/**
	 * Constructor initializes Node Component by initializing ComponentGraphics.
	 * @param MainGui, XLocation, YLocation, Width, Height , NumberOfInputs, NumberOfOutputs
	 * */
	public NodeComponentGraphics(MainGui mg,int originalXLoc,int originalYLoc,int originalWidth,int originalHeight,int numOfInputs,int numOfOutputs){
		super(mg,originalXLoc,originalYLoc,originalWidth,originalHeight,numOfInputs,numOfOutputs);
	}
	@Override
	public void setInputPins(int numOfInputs){
		
		if(numOfInputs<=1){
			if(numOfInputs%2==1){
				for(int i=1;i<=numOfInputs;i++){
					int h = i*originalHeight/(numOfInputs+1)-1;
					ComponentPin pin = new ComponentPin(mg,this,0,h,originalWidth/2,h);
					inputPins.add(pin);
					this.add(inputPins.get(inputPins.size()-1));
				}
			}
			else{
				for(int i=1;i<=numOfInputs;i++){
					int h = (2*(i-1)+1)*originalHeight/(numOfInputs*2) -1;
					ComponentPin pin = new ComponentPin(mg,this,0,h,originalWidth/2,h);
					inputPins.add(pin);
					this.add(inputPins.get(inputPins.size()-1));
				}
			}
		}
		else{
			for(int i=1;i<=numOfInputs;i++){
				int h = originalHeight/(2) -1;
				ComponentPin pin = new ComponentPin(mg,this,0,h,originalWidth/2,h);
				pin.setAsGrouped();
				inputPins.add(pin);
				this.add(inputPins.get(inputPins.size()-1));
			}
		}
		repaint();
	}
	@Override
	public void setOutputPins(int numOfOutputs){
		
		if(numOfOutputs<=1){
			if(numOfOutputs%2==1){
				for(int i=1;i<=numOfOutputs;i++){
					int h = i*originalHeight/(numOfOutputs+1)-1;
					ComponentPin pin = new ComponentPin(mg,this,originalWidth/2,h,originalWidth,h);
					pin.setTypeAsOutput();
					outputPins.add(pin);
					this.add(outputPins.get(outputPins.size()-1));
				}
			}
			else{
				for(int i=1;i<=numOfOutputs;i++){
					int h = (2*(i-1)+1)*originalHeight/(numOfOutputs*2) -1;
					ComponentPin pin = new ComponentPin(mg,this,originalWidth/2,h,originalWidth,h);
					pin.setTypeAsOutput();
					outputPins.add(pin);
					this.add(outputPins.get(outputPins.size()-1));
				}
			}
		}
		else{
			for(int i=1;i<=numOfOutputs;i++){
				int h = originalHeight/(2) -1;
				ComponentPin pin = new ComponentPin(mg,this,originalWidth/2,h,originalWidth,h);
				pin.setTypeAsOutput();
				pin.setAsGrouped();
				outputPins.add(pin);
				this.add(outputPins.get(outputPins.size()-1));
			}
		}
		repaint();
	}
	@Override
	public void adjustPins(){
		
		int inputCount=getNumberOfInputs();
		int outputCount=getNumberOfOutputs();
		
		if(inputCount<=1){
			if(inputCount%2==1){
				for(int i=1;i<=inputCount;i++){
					int h = i*newHeight/(inputCount+1)-1;
					inputPins.get(i-1).zoomAdjust(0,h,newWidth/2,h);
				}
			}
			else{
				for(int i=1;i<=inputCount;i++){
					int h = (2*(i-1)+1)*newHeight/(inputCount*2) -1;
					inputPins.get(i-1).zoomAdjust(0,h,newWidth/2,h);
				}
			}
		}
		else{
			for(int i=1;i<=inputCount;i++){
				int h = newHeight/(2) -1;
				inputPins.get(i-1).zoomAdjust(0,h,newWidth/2,h);
			}
		}
		
		if(outputCount<=1){
			if(outputCount%2==1){
				for(int i=1;i<=outputCount;i++){
					int h = i*newHeight/(outputCount+1)-1;
					outputPins.get(i-1).zoomAdjust(newWidth/2,h,newWidth,h);
				}
			}
			else{
				for(int i=1;i<=outputCount;i++){
					int h = (2*(i-1)+1)*newHeight/(outputCount*2) -1;
					outputPins.get(i-1).zoomAdjust(newWidth/2,h,newWidth,h);
				}
			}
		}
		else{
			for(int i=1;i<=outputCount;i++){
				int h = newHeight/(2) -1;
				outputPins.get(i-1).zoomAdjust(newWidth/2,h,newWidth,h);
			}
		}
		repaint();
	}
}